<template>
	<view class="animal_kind_list">


		<view class="animal_header">
			<view class="search_header_back">
				<image @click="backGo" src="/static/back.png" mode=""></image>
			</view>

			<view class="animal_header_title">
				<view class="th2">Kind your
				</view>
				<view class="th1">Friend</view>
			</view>
			<image aspectFit class="index_header_img" src="/static/kindList-bg.png" mode=""></image>
		</view>
		<view class="animal_main" v-if="kindList">
			<view class="kind_list_item" v-for="(item,index) in kindList">
				<view class="item_left" @click="jumpGo(item.id)">
					<image :src="item.pet_coverURL" mode="aspectFill"></image>
				</view>
				<view class="item-center">
					<view class="tdesc_tit">
						{{item.pet_name}}
					</view>
					<view class="tdesc_bot">
						{{item.pet_engName||"暂无数据"}}
					</view>
					<view class="tdesc_bot">
						{{item.pet_life||"暂无数据"}}
					</view>

				</view>
				<view class="item-right" v-if="kindList">
					<view class="changeCol" @click="changeCol(kindList[index],item['flat'])"
						:class="item.flat? 'active':''">
						<image src="/static/collect.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		kindList,
		judgmentUserCol,
		addCol
	} from "@/api/index.js"
	import row from "../../uni_modules/uview-ui/libs/config/props/row";
	export default {
		data() {
			return {
				num: 0,
				pet_type: 0,
				kindList: [],
				flat: true
			};
		},
		methods: {
			changeCol(idx, flat) {
				if (uni.getStorageSync("uid")) {
					idx.flat = !flat


					let uid = uni.getStorageSync("uid");
					let did = idx.id
					addCol(uid, did).then((content) => {
						console.log(content.data.msg)
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}



			},
			backGo() {
				uni.navigateBack({
					delta: 1
				})
			},
			async selCol(uid, arr) {
				let newArr = Promise.all(
					arr.map((item, index) => {

						return (async () => {
							await judgmentUserCol(uid, item.id).then((content) => {
								arr[index].flat = content.data.flat
							})
							return item
						})()
					})
				).then((res) => {
					console.log(res)
					this.kindList = res
					uni.hideLoading()
				})
			},

			async getList() {
				uni.showLoading({
					title: "加载中..."
				})
				await kindList(this.pet_type, this.num).then((data) => {
					let {
						data: {
							rows
						}
					} = data

					if (rows.length == 0) {
						uni.showToast({
							title: "到底啦 . . .",
							icon: "none"
						})
						this.flat = false
					} else {
						let arr = this.kindList.concat(rows)
						this.selCol(uni.getStorageSync("uid"), arr)
						uni.hideLoading()
						this.flat = true
					}

					console.log(rows)
				})
			},
			jumpGo(id) {
				uni.setStorageSync("id", id)
				uni.navigateTo({
					url: `/pages/detail/detail`
				})
			}
		},
		onReachBottom() {
			console.log(123)
			if (this.flat) {
				this.flat = false
				this.num++
				this.getList()
			}
		},
		onShow() {
			this.pet_type = uni.getStorageSync("pet_type")
			this.getList()
		},
	}
</script>

<style lang="scss">
	.animal_kind_list {
		height: 100vh;
		background-color: #a1b275;
	}

	.animal_search {
		margin: 0 15px;
		height: 50px;
		background-color: white;
		border-radius: 16px;
		display: flex;
		align-items: center;
		background-color: #ffffff;

		.search_icon {
			width: 20px;
			height: 20px;
			margin: 10px;
		}

		.sPlo {
			font-size: 14px;
			color: #BBC3CE;
			font-weight: 500;
		}

	}

	.animal_header {
		width: 100%;
		height: 150px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: relative;

		.search_header_back {
			position: absolute;
			top: 10px;
			left: 10px;
			z-index: 1;

			image {

				width: 30px;
				height: 30px;
			}
		}

	}

	.index_header_img {
		display: block;
		width: 172px;
		height: 100px;
		margin-right: 20px;
		margin-top: 10px;
	}

	.animal_header_title {
		margin-top: 20px;
		margin-left: 40px;
		color: white;
		line-height: 36px;

		.th2 {
			font-size: 28px;
			font-weight: 100;
		}

		.th1 {
			font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
			font-size: 28px
		}
	}

	.animal_main {
		border-radius: 43px 43px 0 0;
		background-color: #f3f2f2;
		min-height: 517px;
		display: flex;
		flex-direction: column;
		align-items: center;

		.kind_list_item {
			width: 333px;
			height: 96px;
			border-radius: 17px;
			background-color: white;
			margin-bottom: 20px;
			display: flex;
			align-items: center;


			&:first-child {
				margin-top: 30px;
			}

			.item_left {
				margin-left: 4px;
				width: 88px;
				height: 88px;
				background-color: lightblue;
				border-radius: 17px;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.item-center {
				height: 70px;
				width: 150px;
				margin-left: 20px;

				.tdesc_tit {
					font-size: 18px;
					color: #333;
				}

				.tdesc_bot {
					margin-top: 6px;
					font-size: 14px;
					color: gray;
					line-height: 14px;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;

					&:last-child {
						margin-top: 2px;
					}
				}
			}

			.item-right {
				height: 88px;
				width: 30px;
				margin-left: 20px;
				display: flex;
				align-items: flex-end;
				justify-content: flex-end;
				margin-bottom: 20px;

				.changeCol {
					width: 22px;
					height: 22px;
					background-color: #ECEBEB;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
					transition: .3s;

					&.active {
						background-color: #ffd967;
						box-shadow: 0 2px 6px #ffd967;
					}

					image {
						width: 11px;
						height: 11px;
					}
				}
			}
		}
	}
</style>